<?php
class CSwfUpload extends CWidget
{
	public $postParams=array();
	public $config=array();
	
    public function run()
    {
        $assets = dirname(__FILE__).'/assets';
        $baseUrl = Yii::app()->assetManager->publish($assets);
        
        $cs = Yii::app()->getClientScript();
		//$cs->registerScript(__CLASS__.'swfuv',"var swfuPath='" . $baseUrl . "';", CClientScript::POS_HEAD);
        $cs->registerScriptFile($baseUrl . '/js/swfupload.js', CClientScript::POS_HEAD);
        $cs->registerScriptFile($baseUrl . '/js/handlers.js', CClientScript::POS_HEAD);
        $cs->registerScriptFile($baseUrl . '/js/fileprogress.js', CClientScript::POS_HEAD);
        $cs->registerCssFile($baseUrl . '/css/swfupload.css');
        $postParams = array('PHPSESSID'=>session_id());
        
		if(isset($this->postParams))
		{
			$postParams = array_merge($postParams, $this->postParams);
		}
		$config = array(
            'post_params'=> $postParams, 
            'flash_url'=>$baseUrl. '/js/swfupload.swf',
            'button_image_url'=> $baseUrl .'/images/SmallSpyGlassWithTransperancy_17x18.png',
            'debug' => true,
        );
		$config = array_merge($config, $this->config);
		$config = CJavaScript::encode($config);
		$cs->registerScript(__CLASS__, "
		var swfu;
        window.onload = function () {
            swfu = new SWFUpload($config);
                $('.btn_startupload').click(function(){
                    swfu.startUpload();
                });
        }
		");
    }
       
}
